<!--
 * @Author: your name
 * @Date: 2021-03-28 14:47:37
 * @LastEditTime: 2021-04-05 20:11:22
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue-admin-template-master\src\views\product\Spu\List.vue
-->

<template>
  <div>
    <el-card>
      <CartgroyList :isShow='isShow'
                    @halderCartList='halderCartList'></CartgroyList>
    </el-card>
    <el-card style="margin-top:20px">
      <div v-show="!isShowSpu && !isShowSku">
        <el-button type="primary"
                   icon="el-icon-plus"
                   @click="addSpuList"
                   :disabled='!cartgroy3Id'>添加SPU</el-button>
        <!-- 列表展示页 -->
        <el-table :data="spuList"
                  border
                  style="width: 100%">
          <el-table-column prop="prop"
                           type="index"
                           label="序号"
                           width="width">
          </el-table-column>
          <el-table-column prop="spuName"
                           label="SPU名称"
                           width="width">
          </el-table-column>
          <el-table-column prop="description"
                           label="SPU描述"
                           width="width">
          </el-table-column>
          <el-table-column prop="prop"
                           label="操作"
                           width="width">
            <template slot-scope="{row,$index}">
              <HintButton type="primary"
                          title="添加SKU"
                          icon="el-icon-plus"
                          size="mini"
                          @click="addSkuList(row)"></HintButton>
              <HintButton type="warning"
                          title="修改SPU"
                          icon="el-icon-edit"
                          size="mini"
                          @click="updataSpuList(row)"></HintButton>
              <HintButton type="info"
                          title="查看SPU的SKU列表"
                          icon="el-icon-info"
                          size="mini"
                          @click="ShowSkuList(row)"></HintButton>
              <el-popconfirm :title="`你确定删除${row.spuName}吗？`"
                             @onConfirm="deleteSpu(row)">
                <HintButton slot="reference"
                            type="danger"
                            icon="el-icon-delete"
                            size="mini"
                            title="删除spu"></HintButton>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>

        <el-pagination :current-page="page"
                       style="text-align:center"
                       :page-sizes="[2, 3, 5]"
                       @size-change="handleSizeChange"
                       @current-change="getSpuList"
                       :page-size="limit"
                       layout=" prev, pager, next, jumper,->,sizes,total"
                       :total="total">
        </el-pagination>
      </div>
      <Spu v-show="isShowSpu"
           ref="spu"
           :visible.sync='isShowSpu'
           @bankSuncess='bankSuncess'
           @cancleBack='cancleBack'></Spu>
      <Sku v-show="isShowSku"
           ref="sku"
           :visible.sync='isShowSku'></Sku>
    </el-card>
    <el-dialog :title="`${spu.spuName}的列表`"
               :visible.sync="dialogTableVisible"
               :before-close="handleClose">
      <el-table :data="skuList"
                v-loading="loading">
        <el-table-column property="skuName"
                         label="名称"
                         width="150"></el-table-column>
        <el-table-column property="price"
                         label="价格"
                         width="200"></el-table-column>
        <el-table-column property="weight"
                         label="重量"></el-table-column>
        <el-table-column property="address"
                         label="默认图片">
          <template slot-scope="{row,$index}">
            <img :src="row.skuDefaultImg"
                 alt=""
                 style="width:100px;height:80px">
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>

</template>

<script>
import Spu from '@/views/product/components/Spu'
import Sku from '@/views/product/components/Sku'
export default {
  name: 'SpuList',
  components: {
    Spu,
    Sku
  },
  //三级列表操作性
  watch: {
    isShowSpu (newVal, oldVal) {
      this.isShow = !newVal
    },
    isShowSku (newVal, oldVal) {
      this.isShow = !newVal
    }
  },
  data () {
    return {
      isShow: true,
      cartgroy1Id: '',
      cartgroy2Id: '',
      cartgroy3Id: '',
      spuList: [],
      page: 1,
      limit: 2,
      total: 0,
      isShowSpu: false,
      isShowSku: false,
      dialogTableVisible: false,
      loading: false,
      spu: {},
      skuList: []
    }
  },
  methods: {
    //三级列表联动
    halderCartList ({ cartgroyId, level }) {
      if (level === 1) {
        this.cartgroy2Id = ''
        this.cartgroy3Id = ''
        this.spuList = []
        this.cartgroy1Id = cartgroyId
      } else if (level === 2) {
        this.cartgroy3Id = ''
        this.spuList = []
        this.cartgroy2Id = cartgroyId
      } else {
        this.cartgroy3Id = cartgroyId
        this.getSpuList()
      }
    },
    //请求Spu列表展示数据
    async getSpuList (pager = 1) {
      this.page = pager
      let { page, limit, cartgroy3Id } = this
      const result = await this.$API.spu.getList(page, limit, cartgroy3Id)
      if (result.code === 200 || result.code === 20000) {
        this.spuList = result.data.records
        this.total = result.data.total
      }
    },
    //选择展示条数
    handleSizeChange (size) {
      this.limit = size
      this.getSpuList()
    },
    //添加SPU
    addSpuList () {
      this.isShowSpu = true
      this.$refs.spu.getSpuListInitData(this.cartgroy3Id)
    },
    //修改SPU
    updataSpuList (row) {
      // this.flag = row.id
      this.isShowSpu = true
      this.$refs.spu.addSpuListInitData(row, this.cartgroy3Id)
    },
    //添加SKU
    addSkuList (row) {
      this.isShowSku = true
      this.$refs.sku.getSkuListInitData(row, this.cartgroy2Id, this.cartgroy3Id)
    },
    //
    bankSuncess (spuInfo) {
      if (spuInfo.id) {
        this.getSpuList(this.page)
      } else {
        this.getSpuList()
      }
    },
    //通知父组件取消回来的
    cancleBack () {

    },
    //删除SPU
    async deleteSpu (row) {
      try {
        await this.$API.spu.remove(row.id)
        this.$message.success('删除成功')
        this.getSpuList(this.spuList.length > 1 ? this.page : this.page - 1)
      } catch (error) {
        this.$message.error('删除失败')
      }
    },
    //查看SPU的SKU列表
    async ShowSkuList (row) {
      this.spu = row
      this.dialogTableVisible = true
      this.loading = true
      const result = await this.$API.sku.getListBySpuId(row.id)
      if (result.code === 200 || result.code === 20000) {
        this.skuList = result.data
      }
      this.loading = false
    },
    handleClose () {
      this.skuList = []
      this.dialogTableVisible = false
      this.loading = false
    }
  }
}
</script>

<style lang="less" scoped>
</style>

